package com.chaochis.wind.config.web_config;

import com.chaochis.wind.config.security_config.SecurityProperties;
import com.chaochis.wind.config.security_config.WebAdminInterceptor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Log4j2
public class WebMvcConfig implements WebMvcConfigurer {

  @Autowired
  private WebAdminInterceptor webAdminFilter;

  @Autowired
  private SecurityProperties securityProperties;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    if (this.securityProperties.isEnable()) {
      this.webAdminFilter.setLoginUrl(this.securityProperties.getLoginUrl());
      var webAdminRegistration = new InterceptorRegistration(this.webAdminFilter);
      webAdminRegistration.addPathPatterns(this.securityProperties.getUrlPatterns());
      webAdminRegistration.excludePathPatterns(this.securityProperties.getIgnoredUrl());
      registry.addInterceptor(this.webAdminFilter);
    } else {
      log.info("您并未开启权限系统!");
    }
  }

}
